package com.tanhua.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.model.domain.Log;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


public interface LogMapper extends BaseMapper<Log> {

    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log WHERE TYPE=#{type} AND log_time=#{logTime}")
    Integer queryByTypeAndLogTime(@Param("type") String type, @Param("logTime") String logTime); //根据操作时间和类型


    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log WHERE log_time=#{logTime}")
    Integer queryByLogTime(String logTime); //展示记录时间查询


    @Select("SELECT COUNT(DISTINCT user_id)  FROM tb_log WHERE log_time=#{today} AND user_id IN (\n " +
            " SELECT user_id FROM tb_log WHERE TYPE=\"0102\" AND log_time=#{yestoday} \n " +
            ")")
    Integer queryNumRetention1d(@Param("today") String today, @Param("yestoday") String yestoday); //查询次日留存


    /**
     * 累计用户
     *
     * @return
     */
    @Select("SELECT COUNT(DISTINCT id) FROM tb_log")
    Integer cumulativeUsers();

    /**
     * 过去30天活跃用户统计
     *
     * @param currentDay
     * @return
     */
    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log\n" +
            "WHERE TIMESTAMPDIFF(DAY,log_time,#{currentDay}) < 31 AND TIMESTAMPDIFF(DAY,log_time,#{currentDay}) >=1")
    Integer activePassMonth(String currentDay);

    /**
     * 过去7天活跃用户统计
     *
     * @param currentDay
     * @return
     */
    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log\n" +
            "WHERE TIMESTAMPDIFF(DAY,log_time,#{currentDay}) < 8 AND TIMESTAMPDIFF(DAY,log_time,#{currentDay}) >=1")
    Integer activePassWeek(String currentDay);

    /**
     * 今日新增用户
     *
     * @param currentDay
     * @return
     */
    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log WHERE type=0102 AND log_time=#{currentDay}")
    Integer newUsersToday(String currentDay);

    /**
     * 昨日新增用户
     * @param currentDay
     * @return
     */
    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log\n" +
            "WHERE TIMESTAMPDIFF(DAY,log_time,#{currentDay}) < 2 AND TIMESTAMPDIFF(DAY,log_time,#{currentDay}) >=1 AND type = 0102")
    Integer yesterdayCount(String currentDay);

    /**
     * 今日登录用户数
     * @param currentDay
     * @return
     */
    @Select("SELECT COUNT(user_id) FROM tb_log WHERE log_time=#{currentDay} AND type=0101")
    Integer loginTimesToday(String currentDay);

    /**
     * 昨日登录用户数
     * @param currentDay
     * @return
     */
    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log\n" +
            "WHERE TIMESTAMPDIFF(DAY,log_time,#{currentDay}) < 2 AND TIMESTAMPDIFF(DAY,log_time,#{currentDay}) >=1 AND type=0101")
    Integer yesterdayLoginCount(String currentDay);

    /**
     * 今日活跃用户
     * @param currentDay
     * @return
     */
    @Select("SELECT COUNT(user_id) FROM tb_log WHERE log_time=#{currentDay}")
    Integer activeUsersToday(String currentDay);

    /**
     * 昨日活跃用户
     * @param currentDay
     * @return
     */
    @Select("SELECT COUNT(user_id) FROM tb_log\n" +
            "WHERE TIMESTAMPDIFF(DAY,log_time,#{currentDay}) < 2 AND TIMESTAMPDIFF(DAY,log_time,#{currentDay}) >=1;")
    Integer yesterdayActive(String currentDay);
}
